//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-07-25 09:11:53 $ $Revision: 1.2 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DText.h"
#include "ofc/DSortedList.h"

#include "DTest.h"

//-Collections-----------------------------------------------------------------

void DSortedList_test()
{
  DSortedList   *list;
  DListIterator *iter;
  DText         *text1 = [DText new];
  DText         *text2 = [DText new];
  DText         *text3 = [DText new];
  DText         *text4 = [DText new];
  DText         *text5 = [DText new];
  DText         *text6 = [DText new];
  DText         *text7 = [DText new];
  DText         *text8 = [DText new];
  DText         *text9 = [DText new];
  DText         *obj;
  
  STARTTEST();
  
  list = [DSortedList new];

  [list class :[DText class]];

  TEST([list ascending]);
  
  [text1 set :"Hello"];    [list insert :text1];
  [text2 set :"!Ready!"];  [list insert :text2];
  [text3 set :"All well"]; [list insert :text3];
  [text4 set :"Zero"];     [list insert :text4];
  [text5 set :"YesYes"];   [list insert :text5];
  [text6 set :"!Ready!"];  [list insert :text6];
  [text7 set :"?Ready?"];  [list insert :text7];
  [text8 set :"Morning"];  [list insert :text8];
  [text9 set :"Evening"];  [list insert :text9];

  TEST(![list isEmpty]);
  TEST([list length] == 9);

  iter = [DListIterator alloc];
  
  [iter init :list];
  
  obj = [iter first];
  
  TEST([iter first] == text2);
  TEST([iter next ] == text6);
  TEST([iter next ] == text7);
  TEST([iter next ] == text3);
  TEST([iter next ] == text9);
  TEST([iter next ] == text1);
  TEST([iter next ] == text8);
  TEST([iter next ] == text5);
  TEST([iter next ] == text4);
  
  [list reverse];

  TEST(![list ascending]);

  TEST([iter first] == text4);
  TEST([iter next ] == text5);
  TEST([iter next ] == text8);
  TEST([iter next ] == text1);
  TEST([iter next ] == text9);
  TEST([iter next ] == text3);
  TEST([iter next ] == text7);
  TEST([iter next ] == text6);
  TEST([iter last ] == text2);

  [list free];

  list = [DSortedList splitSorted :"this/is/a/different/separator/to/split/with" :'/' :5];
  
  TEST([list length] == 6);
  
  TEST([[list get :0] ccompare :"a"] == 0);
  TEST([[list get :1] ccompare :"different"] == 0);
  TEST([[list get :2] ccompare :"is"] == 0);
  TEST([[list get :3] ccompare :"separator"] == 0);
  TEST([[list get :4] ccompare :"this"] == 0);
  TEST([[list get :5] ccompare :"to/split/with"] == 0);
  
  [list free];
  
  STOPTEST();
}

